package com.hk.huakangcompany.controller;

import com.github.pagehelper.PageInfo;
import com.hk.huakangcompany.commons.security.CommonResult;
import com.hk.huakangcompany.model.LoginLog;
import com.hk.huakangcompany.model.vo.Result;
import com.hk.huakangcompany.model.vo.ResultVO;
import com.hk.huakangcompany.service.LoginLogService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @author zhe
 * @Create 2021/7/30/15:22
 * @State 登录日志 前端控制器
 */
@RestController
@RequestMapping("/login-log")
public class LoginLogController {

    @Autowired
    private LoginLogService loginLogService;

    /*@PreAuthorize("hasAuthority('login-log:inquire')")
    @GetMapping("/pageList")
    public ResultVO pageList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10")Integer limit,
            LoginLog loginLog
    ) {
        List<LoginLog> loginLogList = loginLogService.findAll(page, limit, loginLog);
        PageInfo<LoginLog> adminPageInfo = new PageInfo<>(loginLogList);
        int count = loginLogService.count(-1,-1, loginLog);
        if (loginLogList.isEmpty()) {
            ResultVO resultVO = CommonResult.nullResultVO(count,adminPageInfo.getList());
            return resultVO;
        }
        ResultVO resultVO = CommonResult.successResultVO(count,adminPageInfo.getList());
        return resultVO;
    }*/

    /**
     * 1、查询该账户是否正在使用 0：在线中
     * @param id
     * @return
     */
    /*@PreAuthorize("hasAuthority('login-log:delete')")
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) throws IOException {
        LoginLog loginLog = loginLogService.findById(id);
//       判断该账户是否在线中
        if (loginLog.getState() == 0) {
            Result result = CommonResult.nullResult("当前账户在线中，暂不能删除该条日志，请稍后重试！");
            return result;
        }
        loginLogService.delete(id);
        Result result = CommonResult.successResult(null);
        return result;
    }*/

    @ApiOperation("按条删除登录日志")
    @PreAuthorize("hasAuthority('login-log:deleteLimit')")
    @PostMapping("/deleteLimit")
    public Result deleteLimit(Integer count) throws IOException {
        loginLogService.deleteLimit(count);
        Result result = CommonResult.successResult(null);
        return result;
    }




}
